package faircat.android.exphone;

import faircat.android.exphone.ExCallLog.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ExSMS {
	public static final String ACTION_UPDATE_SMS = "faircat.android.exphone.UPDATE_SMS";
	
	public static final String KEY_ID = "_id";
	public static final String KEY_NUMBER = "address";
	public static final String KEY_DATE = "date";
	public static final String KEY_TYPE = "type";
	public static final String KEY_READ = "read";
	public static final String KEY_BODY = "body";
	private static final String TAG = "ExCallLog";
	private static final String DATABASE_NAME = "exphone";
	private static final String DATABASE_TABLE = "sms";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_CREATE =
			"create table "+DATABASE_TABLE+" ("
			+ KEY_ID+" integer primary key autoincrement, "
			+ KEY_NUMBER + " text not null,"
			+ KEY_DATE+" datetime not null,"
			+ KEY_TYPE+" int not null,"
			+ KEY_BODY+" text not null,"
			+ KEY_READ+" boolean not null"
			+ ");";
	
	private final Context context;

	DatabaseHelper mDBHelper;
	SQLiteDatabase mDB;
	
	ExSMS(Context ctx){
		context = ctx;
		mDBHelper = new DatabaseHelper(context);
		mDB = mDBHelper.getWritableDatabase();
	}
	
	public void close(){
		mDBHelper.close();
	}
		
	public long add(String number, long date, int type, int read, String body){
		ContentValues vals = new ContentValues();
		vals.put(KEY_NUMBER, number);
		vals.put(KEY_DATE, date);
		vals.put(KEY_TYPE, type);
		vals.put(KEY_READ, read);
		vals.put(KEY_BODY, body);
		return mDB.insert(DATABASE_TABLE, null, vals);
	}
	public void del(String aa){
		//ContentValues vals = new ContentValues();
		//vals.put(KEY_NUMBER, number);
		//vals.put(KEY_DATE, date);
		//vals.put(KEY_TYPE, type);
		//vals.put(KEY_READ, read);
		//vals.put(KEY_BODY, body);
       // String where = KEY_BODY + " = ?";
      //  String[] whereValue = { Long.toString(aa) };
       // db.delete(TABLE_NAME, where, whereValue);
		
		// mDB.delete(DATABASE_TABLE, where, whereValue);
		
		 
	     String where = KEY_BODY + " = ?";
	        String[] whereValue = { aa };
	       // db.delete(TABLE_NAME, where, whereValue);
			
		 mDB.delete(DATABASE_TABLE, where, whereValue);
		//return mDB.insert(DATABASE_TABLE, null, vals);
	}
	public Cursor queryAll(){
		Cursor cursor = mDB.query(DATABASE_TABLE, 
				new String[]{KEY_ID, KEY_NUMBER, KEY_DATE, KEY_BODY, "count(_id) as message_count"}, 
				null, null, KEY_NUMBER, null, KEY_DATE+" desc");
		return cursor;
	}
	
	public Cursor queryByNumber(String number){
		Cursor cursor = mDB.query(DATABASE_TABLE, 
				new String[]{KEY_ID, KEY_NUMBER, KEY_DATE, KEY_BODY, KEY_TYPE}, 
				KEY_NUMBER+"='"+number+"'", null, null, null, KEY_DATE+" asc");
		return cursor;
	}
	
	public void clear(){
		mDB.delete(DATABASE_TABLE, null, null);
	}
	
	class DatabaseHelper extends SQLiteOpenHelper{
		public DatabaseHelper(Context ctx){
			super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		public void onCreate(SQLiteDatabase db){
			db.execSQL(DATABASE_CREATE);
		}
		
		public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer){
			if( newVer > oldVer ){
				db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
				onCreate(db);
			}
		}
	}
}
